//# sourceURL=http://uMapApp/Module/Module
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth"
    , "../UI/QueryNode"
], function (
    mhjs,
    uMap,
    uEarth,
    QueryNode
) {

    function Module(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        _this.on("loadInit", function () {
            console.log("[模块]" + " : " + _this.title + " > 初始化完成");
        });

        _this.on("open", function () {
            console.log("[模块]" + " : " + _this.title + " > 打开");
        });

        _this.on("close", function () {
            console.log("[模块]" + " : " + _this.title + " > 关闭");
        });


    }

    var _class = Module;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"         : {
            value: function (stopPropagation) {
                if (!this.isOpened) {
                    this.isOpened = true;
                    stopPropagation !== false && this.trigger("open");
                }
            }
        },
        "close"        : {
            value: function (stopPropagation) {
                if (this.isOpened) {
                    this.isOpened = false;
                    stopPropagation !== false && this.trigger("close");
                }
            }
        },
        "toggle"       : {
            value: function (stopPropagation) {
                if (this.isOpened) {
                    this.close(stopPropagation);
                } else {
                    this.open(stopPropagation);
                }
            }
        },
        "objMap"       : {
            set: function (objMap) {
                var _this = this;
                _this._objMap = new mhjs.Map(objMap);
            },
            get: function () {
                return this._objMap;
            }
        },
        "propertiesMap": {
            set: function (propertiesMap) {
                var _this = this;
                _this._propertiesMap = new mhjs.Map(propertiesMap);
            },
            get: function () {
                return this._propertiesMap;
            }
        },
        "load"         : {
            value: function (data, callback) {

                var _this = this;

                var queryNode = _this.app.queryManager.add(new QueryNode({
                    "title"   : "[模块] : " + _this.title,
                    "modality": true
                }));

                queryNode.trigger("send");

                _this.loadMap(function (mapJson) {
                    _this.loadData(data, function (dataJson) {
                        _this.isLoaded = true;
                        _this.trigger("load");
                        queryNode.trigger("loaded");
                        callback && callback(dataJson, mapJson);
                    });
                });

            }
        },
        "loadData"     : {
            value: function (data, callback) {

                var _this = this;
                if (_this.request) {
                    _this.request.send(data, function (json) {
                        if (json.data.list && json.data.list.length > 0) {
                            callback && callback(json.data.list);
                        } else if (json.data.alarms && json.data.alarms.length > 0) {
                            callback && callback(json.data.alarms);
                        } else {
                            callback && callback();
                        }
                    });
                }
            }
        },
        "loadMap"      : {
            value: function (callback) {
                var _this = this;
                if (_this.map) {
                    _this.map.send({}, function (json) {
                        if (json.data) {
                            callback && callback(json.data);
                        }
                    });
                } else {
                    callback && callback();
                }
            }
        },
        "closePanel"   : {
            value: function (stopPropagation) {
                var _this = this;
                _this.closeAllPanel();
                _this.isPanelOpened = false;
                stopPropagation !== false && _this.trigger("closePanel");
            }
        },
        "closeAllPanel": {
            value: function () {
                var _this = this;
                if (_this.panels) {
                    $.each(_this.panels, function (i, panel) {
                        panel.close(false);
                    });
                    if (_this.modules) {
                        $.each(_this.modules, function (i, module) {
                            module.closeAllPanel();
                        });
                    }

                }
            }
        }
    });

    return _class;

});